package com.yilin.tms.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yilin.commons.util.StringUtil;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.data.document.Document;
import com.yilin.tms.core.entity.data.document.DocumentResource;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.data.service.IDocumentResourceService;
import com.yilin.tms.data.service.IDocumentService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 帮助文档目录控制层
 *
 * @author zhangjinyuan
 * @date 2023/08/07  11:07
 */
@RequestMapping(value = "/data/document/")
@RestController
public class DocumentController extends BaseController {

    @Resource
    private IDocumentService documentService;
    @Resource
    private IDocumentResourceService documentResourceService;

    /**
     * 获取帮助文档目录
     */
    @PostMapping(value = "getDocumentList")
    public ReturnData<List<Document>> getDocumentList(Document query) {
        QueryWrapper<Document> qw = new QueryWrapper<Document>().orderByAsc("level");
        if (query.getType() != null && !query.getType()) {
            qw.eq("type", 0);
        }
        List<Document> list = documentService.list();
        return jsonView(true, "获取成功", list);
    }

    /**
     * 获取文档详情
     */
    @PostMapping(value = "getDocument")
    @NotNull("documentId")
    public ReturnData<Document> getDocument(String documentId) {
        Document document = documentService.getById(documentId);
        if (document.getType() && StringUtil.isNotEmpty(document.getResourceId())) {
            DocumentResource resource = documentResourceService.getById(document.getResourceId());
            document.setResource(resource);
        }
        return jsonView(true, "获取成功", document);
    }

    /**
     * 根据ID获取文档
     */
    @PostMapping(value = "getDocumentById")
    @NotNull("documentId")
    public ReturnData<Document> getDocumentById(String documentId) {
        Document document = documentService.getById(documentId);
        return jsonView(true, "获取成功", document);
    }

    /**
     * 获取文档资源
     */
    @PostMapping(value = "getDocumentResource")
    @NotNull("resourceId")
    public ReturnData<DocumentResource> getDocumentResource(String resourceId) {
        DocumentResource resource = documentResourceService.getById(resourceId);
        return jsonView(true, "获取成功", resource);
    }

    /**
     * 添加帮助文档
     */
    @PostMapping(value = "addDocument")
    @NotNull({"name", "type", "parentId", "level"})
    public ReturnData<Object> addDocument(Document document) {
        checkDocumentData(document);
        documentService.addDocument(getLoginUser(), document);
        return jsonView(true, "添加成功");
    }

    /**
     * 修改帮助文档
     */
    @PostMapping(value = "editDocument")
    @NotNull({"id", "name", "type", "parentId", "level"})
    public ReturnData<Object> editDocument(Document document) {
        checkDocumentData(document);
        if (Objects.equals(document.getId(), document.getParentId())) {
            return jsonView(false, "不能选择当前节点为父节点");
        }
        documentService.editDocument(getLoginUser(), document);
        return jsonView(true, "添加成功");
    }

    /**
     * 删除帮助文档
     */
    @PostMapping(value = "deleteDocument")
    @NotNull("documentId")
    public ReturnData<Object> deleteDocument(String documentId) {
        documentService.deleteDocument(documentId);
        return jsonView(true, "操作成功");
    }

    /**
     * 添加帮助文档资源
     */
    @PostMapping(value = "addDocumentResource")
    @NotNull({"documentId", "resource"})
    public ReturnData<Object> addDocumentResource(DocumentResource resource) {
        documentResourceService.addDocumentResource(getLoginUser(), resource);
        return jsonView(true, "添加成功");
    }

    /**
     * 修改帮助文档资源
     */
    @PostMapping(value = "editDocumentResource")
    @NotNull({"id", "documentId", "resource"})
    public ReturnData<Object> editDocumentResource(DocumentResource resource) {
        documentResourceService.editDocumentResource(getLoginUser(), resource);
        return jsonView(true, "修改成功");
    }

    /**
     * 删除帮助文档资源
     */
    @PostMapping(value = "deleteDocumentResource")
    @NotNull({"resourceId"})
    public ReturnData<Object> deleteDocumentResource(String resourceId) {
        documentResourceService.deleteDocumentResource(getLoginUser(), resourceId);
        return jsonView(true, "删除成功");
    }

    /**
     * 检查文档基础数据
     */
    private void checkDocumentData(Document document) {
        if (document.getName().length() > 50) {
            throw new RuntimeException("目录名称不能超过50字符");
        }
        if (StringUtil.isNotEmpty(document.getRemark()) && document.getRemark().length() > 50) {
            throw new RuntimeException("备注不能超过50字符");
        }
    }
}
